package com.ruoyi.threeViolation.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.system.domain.SysDictItem;
import com.ruoyi.system.mapper.SysDictItemMapper;
import com.ruoyi.threeViolation.domain.*;
import com.ruoyi.threeViolation.dto.ViolationEducatingDto;
import com.ruoyi.threeViolation.mapper.*;
import com.ruoyi.threeViolation.vo.ViolationEducatingVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.threeViolation.service.IViolationEducatingService;

/**
 * 三违帮教Service业务层处理
 *
 * @author lth
 * @date 2023-12-18
 */
@Service
public class ViolationEducatingServiceImpl implements IViolationEducatingService
{
    @Autowired
    private ViolationEducatingMapper violationEducatingMapper;

    @Autowired
    private ViolationAssessmentMapper violationAssessmentMapper;

    @Autowired
    private ViolationRegisterMapper violationRegisterMapper;

    @Autowired
    private ViolationTemplateMapper violationTemplateMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysDictItemMapper sysDictItemMapper;

    /**
     * 查询三违帮教
     *
     * @param id 三违帮教主键
     * @return 三违帮教
     */
    @Override
    public ViolationEducating selectViolationEducatingById(String id)
    {
        return violationEducatingMapper.selectViolationEducatingById(id);
    }

    /**
     * 查询三违帮教列表
     *
     * @param violationEducating 三违帮教
     * @return 三违帮教
     */
    @Override
    public List<ViolationEducating> selectViolationEducatingList(ViolationEducating violationEducating)
    {
        return violationEducatingMapper.selectViolationEducatingList(violationEducating);
    }

    /**
     * 新增三违帮教
     *
     * @param violationEducating 三违帮教
     * @return 结果
     */
    @Override
    public int insertViolationEducating(ViolationEducating violationEducating)
    {
        return violationEducatingMapper.insertViolationEducating(violationEducating);
    }

    /**
     * 修改三违帮教
     *
     * @param violationEducating 三违帮教
     * @return 结果
     */
    @Override
    public int updateViolationEducating(ViolationEducating violationEducating)
    {
        return violationEducatingMapper.updateViolationEducating(violationEducating);
    }

    /**
     * 批量删除三违帮教
     *
     * @param ids 需要删除的三违帮教主键
     * @return 结果
     */
    @Override
    public int deleteViolationEducatingByIds(String[] ids)
    {
        return violationEducatingMapper.deleteViolationEducatingByIds(ids);
    }

    /**
     * 删除三违帮教信息
     *
     * @param id 三违帮教主键
     * @return 结果
     */
    @Override
    public int deleteViolationEducatingById(String id)
    {
        return violationEducatingMapper.deleteViolationEducatingById(id);
    }

    /**         查询三违帮教列表
    * @Author lth
    * @Description //TODO
    * @Date 9:52 2023/12/21
    * @Param
    * @return
     **/
    @Override
    public List<ViolationEducatingDto> getList(ViolationEducatingVo vo) {
        //当前对象List
        List<ViolationEducating> violationEducatingList = violationEducatingMapper.selectViolationEducatingList(null);

        List<ViolationEducatingDto> dtoList = violationEducatingList.stream().map(violationEducating -> {
            ViolationEducatingDto violationEducatingDto = new ViolationEducatingDto();
            BeanUtils.copyProperties(violationEducating, violationEducatingDto);

            //assessment
            String violationId = violationEducating.getViolationId();
            ViolationAssessment violationAssessment = violationAssessmentMapper.getByViolationId(violationId);
            if (violationAssessment!=null){
                BeanUtils.copyProperties(violationAssessment, violationEducatingDto);
            }

            //register
            ViolationRegister violationRegister = violationRegisterMapper.selectViolationRegisterById(violationId);
            if (violationRegister!=null){
                BeanUtils.copyProperties(violationRegister, violationEducatingDto);
            }
            //template
            String templateCode = violationRegister.getTemplateCode();
            ViolationTemplate violationTemplate = violationTemplateMapper.getByTemplateCode(templateCode);
            if (violationTemplate!=null){
                BeanUtils.copyProperties(violationTemplate, violationEducatingDto);
            }

            //volationUserName
            String volationUser = violationRegister.getVolationUser();
            SysUser sysUser = sysUserMapper.getById(volationUser);
            violationEducatingDto.setVolationUserName(sysUser.getRealname());

            //sys_dict_item
            //volationLevel_dicText
            String volationLevel = violationTemplate.getVolationLevel();
            SysDictItem level = sysDictItemMapper.getLevelList(volationLevel);
            violationEducatingDto.setVolationLevel_dicText(level.getItemText());
            //volationCategory_dicText
            String volationCategory = violationTemplate.getVolationCategory();
            SysDictItem category = sysDictItemMapper.getCategoryList(volationCategory);
            violationEducatingDto.setVolationCategory_dicText(category.getItemText());

            //ep_resource_team_manage
            //teamLeaderName


            //eduUserName
            String eduUserId = violationEducating.getEduUserId();
            if (eduUserId!=null){
                SysUser user = sysUserMapper.selectSysUserById(eduUserId);
                if (user!=null){
                    violationEducatingDto.setEduUserName(user.getRealname());
                }
            }

            violationEducatingDto.setId(violationEducating.getId());
            return violationEducatingDto;
        }).collect(Collectors.toList());

        return dtoList;
    }
}
